package com.bdsy.business.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.bdsy.api.annotation.IgnoreAuth;
import com.bdsy.business.model.TransactionRecords;
import com.bdsy.business.service.TransactionRecordsService;
import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.Result;
import com.bdsy.system.annotation.SysLog;

/**
 * 交易记录
 * @author MING
 * @date 2018年2月5日 下午5:35:16
 */

@Controller
@RequestMapping(value = {"${web.adminPath}", "${web.appPath}"})
public class TransactionRecordsController {

	@Autowired
	TransactionRecordsService transactionRecordsService;
	
	/**
	 * 根据商品ID查询交易记录信息 
	 */
	@Json
	@RequestMapping(value = "transactionRecordsByShopId")
	public Object transactionRecordsByShopId(int shopId) {
		Map<String, Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> transactionRecords = transactionRecordsService.selectTransactionRecordsByShopId(shopId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", transactionRecords);
		return map;
	}
	
	/**
	 * 添加商品交易记录信息
	 * @param transactionRecords
	 * @return
	 * @author MING
	 * @date 2018年4月25日 下午3:14:23
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "addTransactionRecords")
	public Object addTransactionRecords(TransactionRecords transactionRecords) {
		boolean result = transactionRecordsService.insert(transactionRecords);
		if (result) {
			return Result.ok("addSuccess");
		} else {
			return Result.error("addFail");
		}
	}

	/**
	 * 删除交易记录信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "deleteTransactionRecordsById")
	public Object deleteTransactionRecordsById(String ids) {
		List<String> list = JSON.parseArray(ids, String.class);
		boolean result = transactionRecordsService.deleteTransactionRecordsById(list);
		if (result) {
			return Result.ok("deleteSuccess");
		} else {
			return Result.error("deleteFail");
		}
	}
	
	
	// 后台
	@RequestMapping(value = "transaction")
	public String transaction() {
		return "admin/business/transaction";
	}
	
	@SysLog("根据用户ID查询交易记录信息 ")
	@Json
	@RequestMapping(value = "selectTransactionRecordsByUserId")
	public Object selectTransactionRecordsByUserId(String userId) {
		@SuppressWarnings("rawtypes")
		List<Map> transactionRecordsList = transactionRecordsService.selectTransactionRecordsByUserId(userId);
		return transactionRecordsList;
	}
	
}
